<?php
$logger->info('Start=======================================home page=======================================Start');
$logger->info('Memory Usage: ' . memory_get_usage());

$xmlSections = plexGetSections();
$sectionKeys = $xmlSections->xpath('//Directory[@type="movie"]/@key');

$numRandom = rand(0, count($sectionKeys) - 1);
$logger->debug('Section Random Number: ' . $numRandom);
$xmlMedia = plexGetMedia((string) $sectionKeys[$numRandom]['key']);
$backgroundNodes = $xmlMedia->xpath('//Video[@art]');
$numRandom2 = rand(0, count($backgroundNodes) - 1);
$backgroudImgPath = (string) $backgroundNodes[$numRandom2]['art'];
$width = $browser->Resolution['tv']['width'];
if (empty($width))
    $width = '1280';
$height = $browser->Resolution['tv']['height'];
if (empty($height))
    $height = '720';

$cacheFolder = 'menu/backdrop';

$backgroudImgPath = plexGetImage($config['plex_url'] . $backgroudImgPath, $width, $height, $cacheFolder);
//$backgroudImgPath = $config['plex_url'] . '/photo/:/transcode?width=' . $width . '&height=' . $height . '&url=' . urlencode($config['plex_url'] . $backgroudImgPath);
$logger->info('$backgroudImgPath: ' . $backgroudImgPath);

$backgroundImg = img_overlay($backgroudImgPath, $homePath . '/images/menu/overlay/home_menu.png', $cacheFolder, 0, 515);

($_SESSION['resolution'] == "SD") ? $display['mainwidth'] = 624 : $display['mainwidth'] = 1100;
?>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="screen" href="<?php echo $homePath . "/css/meun.css.php" ?>" />
</head>

<body bgcolor="<?php echo $_SESSION['bgcolor']; ?>" text="#000000" background="<?php echo $backgroundImg; ?>" FOCUSCOLOR="transparent" onloadset="" marginwidth=0 marginheight=0>
    <?php
    $cellrowmiddleheight = 52;
    if ($_SESSION['resolution'] == "SD") {
        $cellrowmiddleheight = 66;
    }
    ?>
    <table id="main" width="<?php echo $display["mainwidth"]; ?>" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td colspan="6" height="484" align="center" valign="bottom">
        </tr>
        <tr>
            <td colspan="6" height="31" align="right" valign="middle"><b><font color="#ffffff"><?php echo date("l, d. F Y"); ?></font></b></td>
        </tr>
        <tr>
            <td height="52" colspan="6">
                <table height="52" width="<?php echo $display["mainwidth"]; ?>" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <?php
                        $maxItems = $xmlSections['size'];
                        $tdWidth = 100 / $maxItems;
                                $item = 1;
                        //$sectionNodes = $xmlSections->xpath('//Directory');
                        //$maxItems = count($sectionNodes);
                        foreach ($xmlSections->Directory as $section) {
                            if ($item == 1) {
                                $currentBtn = 'btn' . $item;
                                $rightBtn = 'btn' . ($item + 1);
                                $leftBtn = 'btn' . $maxItems;
                            } elseif ($item == $maxItems) {
                                $currentBtn = 'btn' . $item;
                                $rightBtn = 'btn' . 1;
                                $leftBtn = 'btn' . ($item - 1);
                            } else {
                                $currentBtn = 'btn' . $item;
                                $rightBtn = 'btn' . ($item + 1);
                                $leftBtn = 'btn' . ($item - 1);
                            }
                            echo "<td height=$cellrowmiddleheight width=$tdWidth% align=\"center\">";
                            echo "	<a href=\"?destination=" . $section['type'] . "&page=wall&key=" . $section['key'] . "&category=all&index=1\" name=\"$currentBtn\" ONKEYLEFTSET=\"$leftBtn\" ONKEYRIGHTSET=\"$rightBtn\">";
                            echo "		<img id=\"menu1\" src=\"" . img_text($section['title'], 20, 240, 240, 240) . "\" onfocussrc=\"" . img_text($section['title'], 20, 255, 215, 0) . "\">";
                            echo "	</a>";
                            echo "</td>";
                            $item++;
                        }
                        ?>
                    </tr>
                </table>
            </td>
        </tr>
        <?php
        $str = 'Recently Added: ';
        foreach ($backgroundNodes as $video) {
            $str = $str . $video['title'] . ", ";
        }
        $str = rtrim($str, ", ");
        $logger->info("Device Name: " . $browser->Device . " | Resolution Display: " . $browser->Resolution['display'] . " | Resolution TV: " . $browser->Resolution['tv']['width'] . "x" . $browser->Resolution['tv']['height'] . " | Resolution Browser: " . $browser->Resolution['browser']['width'] . "x" . $browser->Resolution['browser']['height'] . " | User Agent String: " . $browser->UserAgent);
        ?>
        <tr>
            <td colspan="5" width="1000" height="32">
        <marquee width="760" scrollamount="2">
            <font color="#ffffff">
            <b><?php echo $str; ?> </b>
            </font>
        </marquee>
    </td>
</tr>
<tr>
    <td colspan="6" height="32">&nbsp;</td>
</tr>
</table>
<div id="div_info">plexNMT V<?php echo $config['version'] . " | " . $backgroundNodes[$numRandom2]['title'] ?></div>

</body>
<?php
unset($xmlSections);
unset($backgroundNodes);
unset($backgroudImgPath);
unset($sectionKeys);
unset($numRandom);
$logger->info('Memory Usage: ' . memory_get_usage());
$logger->info('End=======================================home page=======================================End');
?>